package com.aos;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;


public class ConfigReader {
	// read configuration file
	public static void readConfiguration() {
		Node.neighbors = new HashMap<Integer, NodeInfo>();
		Node.neighborIDList = new ArrayList<Integer>();
		NodeInfo nInfo;

		String line;
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new DataInputStream(new FileInputStream(System
							.getProperty("user.dir") + "/config.txt"))));
			// int newlineCount = 0;
			String[] items;
			boolean hasGetTotalNum = false;
			while ((line = br.readLine()) != null) {
				String firstVal = line.split("#")[0].trim();

				if (!firstVal.isEmpty()) {
					String[] spVals = firstVal.split(" ");
					if ((spVals.length==1) & (!hasGetTotalNum)) {
						Node.totalNodes = Integer.parseInt(firstVal);
						hasGetTotalNum = true;
						continue;
					}

					if ((spVals.length==1) & (hasGetTotalNum)) {
						Node.dNode = Integer.parseInt(firstVal);
						continue;
					}

					if (Character.isDigit(firstVal.charAt(0))
							& (spVals.length>1)) {
						nInfo = new NodeInfo(firstVal);
						if (isHost(nInfo)) {
							Node.nodeInfo = nInfo;
						} else {
							Node.neighbors.put(nInfo.getID(), nInfo);
						}
						continue;
					}

					if (!Character.isDigit(firstVal.charAt(0))
							& (spVals.length>1)) {
						Pattern r = Pattern.compile("\\)\\(|\\(|\\)");
						items = r.split(firstVal);
						updateNeighborList(items);
						continue;
					}
				}

			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void updateNeighborList(String[] items) {
		for (int i = 0; i < items.length; i++)
		{
			if (!items[i].trim().isEmpty()) {
				String[] pair = items[i].split(" ");
				if (Integer.parseInt(pair[0]) == Node.nodeInfo.getID()) {
					Node.neighborIDList.add(Integer.parseInt(pair[1]));
				} else if (Integer.parseInt(pair[1]) == Node.nodeInfo.getID()) {
					Node.neighborIDList.add(Integer.parseInt(pair[0]));
				}
			}
		}

		ArrayList<Integer> removeList = new ArrayList<Integer>();
		
		for (Integer id : Node.neighbors.keySet()) {
			if (!Node.neighborIDList.contains(id)) {
				removeList.add(id);
				//Node.neighbors.remove(id);
			}
		}
		for(int i : removeList)
		{
			Node.neighbors.remove(i);
		}
	}

	public static boolean isHost(NodeInfo nodeInfo) {
		boolean host = false;
		InetAddress addr;
		try {
			addr = InetAddress.getLocalHost();
			// String hostname = addr.getHostName();

			if (nodeInfo.getAddress().equals(addr.getHostName()) & (nodeInfo.getPort() == Node.port)) {
				host = true;
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

		return host;
	}
}
